package com.bjesc_b2c.app.appraiset.mvp.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.adapter.OrderTeackAdapter;
import com.bjesc_b2c.app.appraiset.base.BaseFragment;
import com.bjesc_b2c.app.appraiset.http.IApiFragmentRequest;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.CustomerDetailsActivity;
import com.bjesc_b2c.app.appraiset.utils.CustomSharedPreferences;
import com.bjesc_b2c.app.appraiset.widget.WrapContentLinearLayoutManager;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.widget.SpringView;
import com.orhanobut.logger.Logger;
import com.rxretrofit_build.util.MapUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import app.pinggushi.bjesc.com.baserecyclerview.adapter.MultiItemTypeAdapter;
import butterknife.ButterKnife;
import master_bean.OrderTrackBean;
import rxretrofit_base.model.KeyBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;

/**
 * Created by yidong on 2017/5/15.
 *
 * 接单线索
 */

public class FragmentCarClue extends BaseFragment implements SpringView.OnFreshListener, MultiItemTypeAdapter.OnItemClickListener {
    RecyclerView recyclerView;
    SpringView springView;
    private OrderTeackAdapter mdapter ;
    private List<OrderTrackBean> orderList = new ArrayList<OrderTrackBean>();
    private int page = 1;
    private boolean isLodmore = true;
    private String fBrandId = "";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = inflater.inflate(R.layout.fragment_todayplan, null);
        ButterKnife.inject(this, super.onCreateView(inflater, container, savedInstanceState));
        initViews();
        return super.onCreateView(inflater, container, savedInstanceState);
    }
    private void initViews() {
        recyclerView = (RecyclerView) contentView.findViewById(R.id.recycler_view);
        springView = (SpringView) contentView.findViewById(R.id.spring_view);
        springView.setListener(this);
        springView.setHeader(new DefaultFooter(getActivity()));
        springView.setFooter(new DefaultFooter(getActivity()));
        springView.setType(SpringView.Type.FOLLOW);
        LinearLayoutManager layoutManager = new WrapContentLinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        getHistoryData("0", page);
    }
    
    private void refreshData(List<OrderTrackBean> data) {
        if (page == 1) {
            orderList.clear();
        }
        orderList.addAll(data);
        Logger.d("historydata====" + orderList.size());
        if (mdapter == null) {
            mdapter = new OrderTeackAdapter(getActivity(), R.layout.layout_item_oeder_track, orderList);
            mdapter.setOnItemClickListener(this);
            recyclerView.setAdapter(mdapter);
        } else {
            mdapter.refresh(orderList);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            getHistoryData("0", page);
        }
    }
    /**
     * @param view
     * @param holder
     * @param position
     */
    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        CustomSharedPreferences.saveString(getActivity(), new MapUtil().put(KeyBean.ORDER_ID.valueOf(), orderList.get(position).getId()).build(), KeyBean.FILE_NAME.valueOf());
        CustomSharedPreferences.saveString(getActivity(), new MapUtil().put(KeyBean.TOU_ID.valueOf(), orderList.get(position).getTou_id()).build(), KeyBean.FILE_NAME.valueOf());
        startIntent(new Intent(getActivity(), CustomerDetailsActivity.class));
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }

    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
        if (isLodmore) {
            page++;
            getHistoryData("0",page);
        }else{
            springView.onFinishFreshAndLoad();
        }
        Logger.d("page==" + page);

    }

    /**
     * 上拉加载
     */
    @Override
    public void onLoadmore() {
        if (isLodmore) {
            page++;
            getHistoryData("0",page);
        }else{
            springView.onFinishFreshAndLoad();
        }
    }

    private void getHistoryData(String status, final int pag) {
        HashMap<String, Object> map = new MapUtil().
                put("status", status).
                put("page", pag).build();
        RxRequest.request(ShowType.SHOW, RxRequest.getClient(getActivity(), map).create(IApiFragmentRequest.class).orderList(),
                new BaseSubscriber<TokenBean<List<OrderTrackBean>>>(getActivity()) {
                    @Override
                    public void onSuccess(TokenBean<List<OrderTrackBean>> o) {
                        springView.onFinishFreshAndLoad();
                        if (o.getData() != null && o.getData().size() > 0) {
                            refreshData(o.getData());
                            isLodmore = true;
                        } else {
                            showToast("没有数据！");
//                          springView.setEnable(false);
                            page =1;
                            isLodmore = false;
                        }
                    }
                    @Override
                    public void onFailure(Throwable e) {
                        springView.onFinishFreshAndLoad();
                    }
                });
    }

}